<?php
// +----------------------------------------------------------------------
// | 海豚PHP框架 [ DolphinPHP ]
// +----------------------------------------------------------------------
// | 版权所有 2016~2019 广东卓锐软件有限公司 [ http://www.zrthink.com ]
// +----------------------------------------------------------------------
// | 官方网站: http://dolphinphp.com
// +----------------------------------------------------------------------

namespace app\index\controller;

use think\Db;
/**
 * 前台首页控制器
 * @package app\index\controller
 */
class Index extends Home
{
    public function __construct()
    {
        parent::__construct();
        $this->checkLogin();
    }
    public function index()
    {
        // 默认跳转模块
        if (config('home_default_module') != '' && config('home_default_module') != 'index') {
            $this->redirect(config('home_default_module'). '/index/index');
        }
        return '<style type="text/css">*{ padding: 0; margin: 0; } .think_default_text{ padding: 4px 48px;} a{color:#2E5CD5;cursor: pointer;text-decoration: none} a:hover{text-decoration:underline; } body{ background: #fff; font-family: "Century Gothic","Microsoft yahei"; color: #333;font-size:18px} h1{ font-size: 100px; font-weight: normal; margin-bottom: 12px; } p{ line-height: 1.6em; font-size: 42px }</style><div style="padding: 24px 48px;"> <h1>:)</h1><p> '.config("dolphin.product_name").' '.config("dolphin.product_version").'<br/><span style="font-size:30px">极速 · 极简 · 极致</span></p></div>';
    }

    public function getInfo()
    {
        // dg($this->wxapp_id);
        $wxapp = Db::name('admin_wxapp')->find($this->wxapp_id);
        $wxapp['intention'] = str_replace('/uploads/images',config('base_url').'/uploads/images',$wxapp['intention']);
        $wxapp['rules'] = str_replace('<img src="/uploads/images','<img src="'.config('base_url').'/uploads/images',$wxapp['rules']);
        $wxapp['wechat_img'] = $this->getImage($wxapp['wechat_img']);

        //==二维码信息==//
        $qrcodeList = Db::name('admin_qrcode')->where(['wxapp_id'=>$this->wxapp_id,'status'=>1])->select();
        foreach($qrcodeList as $k=>$row){
            $qrcodeList[$k]['qrcode'] = $this->getImage($row['qrcode']);
        }

        //==推荐小程序==//
        $recommend = Db::name('admin_recommend')->where(['wxapp_id'=>$this->wxapp_id,'status'=>1])->find();
        if(!empty($recommend)){
            $recommend['wx_qrcode'] = $this->getImage($recommend['wx_qrcode']);
            $recommend['images'] = $this->getImage($recommend['images']);
        }else{
            $recommend = [];
        }
        

        //==轮播图==//
        $bannerList = Db::name('admin_banner')->field('images,title')->where('wxapp_id',$this->wxapp_id)->select();
        foreach($bannerList as $k=>$row){
            $bannerList[$k]['images'] = $this->getImage($row['images']);
        }

        //==一些东西==//
        $kf['text'] = '推荐添加微信客服，了解更多详情';
        $kf['time'] = '客服在线时间: 6:00~23:00';
        $kf['save'] = '保存';
        $kf['st'] = 0;
        $config = [
            'title'=>'中哥语录',
            'desc'=>'',
            'save'=>'保存',
            'ad_id'=>'adunit-c83e1388663a8af1',
            'banner_ad'=>'adunit-9f4bcb3eadc7052c',
        ];
        $data = [
            'recommend'=>$recommend,
            'qrcodeList'=>$qrcodeList,
            'wxapp'=>$wxapp,
            'bannerList'=>$bannerList,
            'config'=>$config,
            'kf'=>$kf,
        ];
        $this->returnJson(10001,'请求成功',$data);
    }

    public function saveUserinfo()
    {
        $data = $this->request->param('userinfo');
        if(empty($data)){
            $this->returnJson(-10002,'授权失败',[],$data);
        }
        $res = Db::name('wx_user')->where('id',$this->userId)->update([
            'nickname'=>$data['nickName'],
            'city'=>$data['city'],
            'province'=>$data['province'],
            'country'=>$data['country'],
            'headimgurl'=>$data['avatarUrl'],
        ]);
        $this->returnJson(10002,'授权成功',$data,$data);
    }

    public function getQrcode()
    {
        $qrcodeList = Db::name('admin_qrcode')->where(['wxapp_id'=>$this->wxapp_id,'status'=>1])->select();
        foreach($qrcodeList as $k=>$row){
            $qrcodeList[$k]['qrcode'] = $this->getImage($row['qrcode']);
        }
        $this->returnJson(10001,'请求成功',$qrcodeList);
    }

    public function getRecommend()
    {
        $recommend = Db::name('admin_recommend')->where(['wxapp_id'=>$this->wxapp_id,'status'=>1])->find();
        // dump($recommend);
        $recommend['wx_qrcode'] = $this->getImage($recommend['wx_qrcode']);
        $recommend['images'] = $this->getImage($recommend['images']);
        $this->returnJson(10001,'请求成功',$recommend);
    }
}
